package homework7_30;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

/*
 * ��ҵ4�����ɵ�50��������50��Ů��.ÿһ����Ϣռ��һ�С�
׼��һ���ļ�����������ʮ��ѧ����
ÿһ��ѧ��ֻ�ܱ��㵽һ�Σ���������10�Σ����һ�֡�
��11�����е�ʱ�򣬲���Ҫ�ֶ����������ļ���Ҫ������Զ���ʼ�ڶ��ֵ���

 */
public class java06 {

    static class Student {
        String name;
        String gender;
        int age;

        public Student(String name, String gender, int age) {
            this.name = name;
            this.gender = gender;
            this.age = age;
        }

        public String toString() {
            return name + "-" + gender + "-" + age;
        }
    }

    public static void main(String[] args) {
        List<Student> students = readStudentsFromFile("name2.txt");
        if (students == null || students.isEmpty()) {
            System.out.println("文件中没有学生信息或读取文件出错");
            return;
        }

        try {
            OutputStreamWriter writer = new OutputStreamWriter(System.out, "utf-8");
            Random random = new Random();
            Set<String> pickedStudents = new HashSet<>();
            int rounds = 10;

            for (int round = 1; round <= rounds; round++) {
                writer.write("第 " + round + "轮开始:\n");
                while (true) {
                    int index = random.nextInt(students.size());
                    Student student = students.get(index);
                    if (!pickedStudents.contains(student.toString())) {
                        pickedStudents.add(student.toString());
                        writer.write("点到的学生: " + student.toString() + "\n");
                        break;
                    }
                }
            }

            writer.write("第 11 轮开始:\n");
            pickedStudents.clear();

            for (int round = 1; round <= rounds; round++) {
            	writer.write("第 " + round + " 轮开始:\n");
                while (true) {
                    int index = random.nextInt(students.size());
                    Student student = students.get(index);
                    if (!pickedStudents.contains(student.toString())) {
                        pickedStudents.add(student.toString());
                        writer.write("点到的学生: " + student.toString() + "\n");
                        break;
                    }
                }
            }

            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static List<Student> readStudentsFromFile(String filename) {
        List<Student> students = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("-");
                if (parts.length == 3) {
                    String name = parts[0];
                    String gender = parts[1];
                    int age = Integer.parseInt(parts[2]);
                    students.add(new Student(name, gender, age));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return students;
    }
}